package com.example.jussi.myproject;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class UpdateCard extends AppCompatActivity {
    CardBean card;
    EditText businessName, name, job, email, phoneNum, qqNum, info, addr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_card);
        businessName = (EditText) findViewById(R.id.updateBusinessName);
        name = (EditText) findViewById(R.id.updateName);
        job = (EditText) findViewById(R.id.updateJob);
        email = (EditText) findViewById(R.id.updateEmail);
        phoneNum = (EditText) findViewById(R.id.updatePhoneNum);
        qqNum = (EditText) findViewById(R.id.updateQQNum);
        info = (EditText) findViewById(R.id.updateInfo);
        addr = (EditText) findViewById(R.id.updateAddress);
        initView();
    }

    private void initView() {
        Intent data = getIntent();
        card = (CardBean) data.getSerializableExtra("card");
        businessName.setText(card.getBusinessName());
        name.setText(card.getName());
        job.setText(card.getJob());
        email.setText(card.getEmail());
        phoneNum.setText(card.getPhoneNum());
        qqNum.setText(card.getQqNum());
        info.setText(card.getInfo());
        addr.setText(card.getAddr());
    }

    public void updateCommit(View view){
        if(businessName.getText() == null || "".equals(businessName.getText().toString())
                || name.getText() == null || "".equals(name.getText().toString())
                || phoneNum.getText() == null || "".equals(phoneNum.getText().toString())){
            Toast.makeText(this, "公司名称、姓名、电话\n  不能为空！！！", Toast.LENGTH_SHORT).show();
            return;
        }
        card.setBusinessName(businessName.getText().toString());
        card.setName(name.getText().toString());
        card.setJob(job.getText().toString());
        card.setEmail(email.getText().toString());
        card.setPhoneNum(phoneNum.getText().toString());
        card.setQqNum(qqNum.getText().toString());
        card.setInfo(info.getText().toString());
        card.setAddr(addr.getText().toString());
        Intent intent = new Intent();
        intent.putExtra("card", card);
        setResult(RESULT_OK, intent);
        finish();
    }

    public void updateCancel(View view){
        Intent intent = new Intent();
        setResult(RESULT_CANCELED, intent);
        finish();
    }
}
